<?php
/**
* Change logs
* Apr 3, 2011 - trungdt - Add getMediaPlayer
*/
class albummodule {
	function name() { return "Album module"; }
	function description() { return "Album management"; }
	function author() { return 'immanuel192'; }
	
	function hasSources() { return true; }
	function hasContent() { return true; }
	function hasViews() { return true; }
	
	function supportsWorkflow() { return false; }
	
	function permissions($internal = '') {
		return array(
			'administrate'=>'Administrate',
			'manage'=>'Manage Album',
			'configure'=>'Configure'
		);
	}
		
	function show($view,$loc = null, $title = '') {
		$_REQUEST['page']=1;
		$_REQUEST['flow_has_set']=1; // to compatipility with view_page
		exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_SECTIONAL);
		include("actions/view_page.php");
	}
	
	function deleteIn($loc) {
		// IMPLEMENTME:deleteIn for the listing module
	}
	
	function copyContent($oloc,$nloc) {
		// IMPLEMENTME:copyContent for the listing module
	}

	function searchName() {
	    $lang=exponent_lang_loadFile("modules/albummodule/class.php");
			return $lang['search_name'];
	}
	
	/*
	   8/26/2009 immanuel192
	   This function create HTML content of spider item
	*/
	function spiderTemplate($album)
	{
	   global $db;
	   if ($album->image_id == 0) {
				$album->picpath = '';
			} else {
				$file = $db->selectObject('file', 'id='.$album->image_id);
				$album->picpath = $file->directory.'/'.$file->filename;
			}
      $i18n=exponent_lang_loadFile("modules/albummodule/views/Default.php");			
			// for rating
			$rate_1 = floor($album->rate);
			$rate_half = $album->rate - $rate_1;
      $rate_2 = 5- $rate_1 - (($rate_half > 0)?1:0);
      $rate=array();
      for ($j=1;$j<=$rate_1;$j++)
        $rate[$j]=1; // for active rate
      if ($rate_half > 0)
        $rate[$rate_1+1]=3; // for rate half
      for ($j=5-$rate_2+1;$j<=5;$j++)
        $rate[$j]=2; // for active rate
      $album->rate=$rate;
			// end of rating
			
			// for format_type
		  $temp=$db->selectObject("album_format","id = ".$album->format);
		  $album->format_text = $temp->name;
			// end of format type
			// for genre 
      $temp=$db->selectObject("album_genre","id = ".$album->genre);
  	  $album->genre_text = $temp->name;
			// end of genre
			
			// count all music in this album
			$album->musics=$db->countObjects('musics','album_id='.$album->id);
			
			// create template content
			$template=new template("albummodule","_search");
			$template->assign("album",$album);
			ob_start();
			$template->output();
			$get_value=ob_get_contents();
			ob_end_clean(); 
			return $get_value;
  }
	
	
	function spiderContent($item = null) {
		global $db;
		
		if (!defined('SYS_SEARCH')) require_once(BASE.'subsystems/search.php');
		
		$search = null;
		$search->category = 'Albums';
		$search->ref_module = 'albummodule';
		$search->ref_type = 'album';
		
		if ($item) {
			$db->delete('search',"ref_module='albummodule' AND ref_type='album' AND original_id=" . $item->id);
			$search->original_id = $item->id;
			$search->title = ' ' . $item->name . ' ';
			$search->view_link = 'index.php?module=albummodule&action=view&id='.$item->id;
			$search->body = albummodule::spiderTemplate($item); 
			$search->dont_trim=1;
			$search->keywords=$item->publisher;
			$search->location_data = $item->location_data;
			$db->insertObject($search,'search');
		} else {
			$db->delete('search',"ref_module='albummodule' AND ref_type='album'");
			foreach ($db->selectObjects('album',"") as $item) {
				$search->original_id = $item->id;
				$search->title = ' ' . $item->name . ' ';
				$search->view_link = 'index.php?module=albummodule&action=view&id='.$item->id;
				$search->body = albummodule::spiderTemplate($item); 
			 $search->dont_trim=1;
				$search->keywords=$item->publisher;
				$search->location_data = $item->location_data;
				$db->insertObject($search,'search');
			}
		}
		
		return true;
	}
    
    /**
        * Get media player 
        * 
        * @author: trungdt
        * @since: Mar 20, 2011
        * @param mixed $music_item : Music Record, query from DB
        * @param mixed $type : =1 For homepage, =0: for normal album module detail view
        * @param bool auto_start 
        * @param mixed $force_width : Force player width, default is 400
        * @param mixed $force_height : Force player height, default is 320
        * 
        * @return string html text
        */
        function getMediaPlayer($musics, $type=0, $auto_start=1, $force_width=-1, $force_height=-1){
            global $db;
            
            if ($auto_start == 1)
            {
                $auto_start='true';
            }
            else
            {
                $auto_start='false';
            }
            
            if ($musics->embed_code != '')
            {
                $musics->high_quality=$musics->embed_code;
            }
            else{
                if ($musics->high_quality <= 0) {
                    $musics->high_quality = '';
                } else {            
                    $file = $db->selectObject('file', 'id='.$musics->high_quality);
                    $musics->high_quality_id=$musics->high_quality;
                    $musics->high_quality = $file->directory.'/'.$file->filename;
                }
            }

            $ret="";
            // calculate media size
            $width = 400;
            $height = 320;
            if ($type==1)
            {
                $width=188;
                $height=200;
            }
            if ($force_width > -1)
            {
                $width=$force_width;
            }
            if ($force_height > -1)
            {
                $height=$force_height;
            }
            
            $ret="";
            
            if ($musics->file_type == 0)
            {
                // FLV - Flash
                $ret='<embed height="'.$height.'" width="'.$width.'" flashvars="height='.$height.'&amp;width='.$width.'&amp;autostart='.$auto_start.'&amp;file=/'.$musics->high_quality.'&amp;javascriptid=myplayer&amp;searchbar=false&amp;usefullscreen=true&amp;enablejs=true&amp;linktarget=_self" allowfullscreen="true" allowscriptaccess="always" quality="high" name="myplayer" id="myplayer" style="" src="'.THEME_RELATIVE.'/mediaplayer.swf" type="application/x-shockwave-flash">';
            }
            else if ($musics->file_type == 1)
                {
                    // Embeb code
                    $ret=$musics->embed_code;
                    $ret= preg_replace("/height=\\\"\d+\\\"/", "height='".$height."'", preg_replace("/width=\\\"\d+\\\"/", "width='".$width."'", $ret));
                    $html= preg_replace("/height=\\\'\d+\\\'/", "height='".$height."'", preg_replace("/width=\\\'\d+\\\'/", "width='".$width."'", $ret));
                }
                else if ($musics->file_type == 2)
                    {
                        $url=$musics->high_quality;

                        // Silverlight - Media player
                        $ret='<object height="'.$height.'" width="'.$width.'" type="application/x-oleobject" standby="Loading Microsoft Windows Media Player components..." codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="PlayerWP"><param value="false" name="enableContextMenu">
                        <param value="true" name="Prebuffer">
                        <param value="true" name="enabled">
                        <param value="true" name="windowlessvideo">
                        <param value="0" name="autosize">
                        <param value="10" name="playCount">
                        <param value="0" name="fullScreen">
                        <param value="0" name="stretchToFit">
                        <param value="true" name="transparentatStart">
                        <param value="false" name="AllowChangeDisplaySize">
                        <param value="'.$auto_start.'" name="autoStart">
                        <param value="false" name="showControls">
                        <param value="full" name="uimode">
                        <param value="false" name="mute">
                        <param value="'.$url.'" name="Url">
                        <embed AllowChangeDisplaySize="false" height="'.$height.'" width="'.$width.'" playcount="10" autostart="'.$auto_start.'" src="'.$url.'" uimode="full" fullscreen="0" stretchtofit="0" enablecontextmenu="0" videoborder3d="-1" showstatusbar="-1" showdisplay="0" showtracker="-1" showcontrols="0" bgcolor="darkblue" autosize="0" name="Player" id="Player" pluginspage="http://microsoft.com/windows/mediaplayer/en/download/" type="application/x-mplayer2">
                        </object>';
                    }
                    else //if ($musics->file_type == 3) // default is MP3
                        {
                            // MP3 player
                            $width=300;
                            $height=20;
                            $ret='<embed height="'.$height.'" width="'.$width.'" flashvars="height='.$height.'&amp;width='.$width.'&amp;autostart='.$auto_start.'&amp;file=/'.$musics->high_quality.'&amp;javascriptid=myplayer&amp;searchbar=false&amp;usefullscreen=true&amp;enablejs=true&amp;linktarget=_self" allowfullscreen="true" allowscriptaccess="always" quality="high" name="myplayer" id="myplayer" style="" src="'.(THEME_RELATIVE).'/mediaplayer.swf" type="application/x-shockwave-flash">';

                        }
            return $ret;
        }
}

?>
